// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router/lazy'
import 'ant-design-vue/dist/antd.css'
import Antd from 'ant-design-vue'
import Viser from 'viser-vue'
import store from './store'
import VueRouter from "vue-router";
import {auth} from './utils/auth'

Vue.use(Viser);
Vue.use(Antd);
Vue.use(VueRouter);

Vue.config.productionTip = false;

const whiteList = ['/login']; // no redirect whitelist

router.beforeEach((to, from, next) => {

  if (auth.getToken()) {
    if (!store.state.install) {
      store.dispatch('generateRoutes').then(() => { // 根据roles权限生成可访问的路由表
        console.log(store.getters);
        router.addRoutes(store.state.addRoutes);// 动态添加可访问路由表
        store.commit("installRouter");
        next({...to, replace: true}) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
      });
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      next()
    } else {
      next('/login')
    }
  }
});


new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
});
